//
//  ViewController.swift
//  ObjectiveCDemo
//
//  Created by XuelongWen on 2025/5/19.
//
import UIKit

class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    
    // 设置背景颜色
    view.backgroundColor = .white
    
    // 创建 3 个 UILabel
    let label1 = createLabel(text: "a1234", backgroundColor: .red)
    let label2 = createLabel(text: "a1234567", backgroundColor: .green)
    let label3 = createLabel(text: "a1234567890123", backgroundColor: .blue)
    
    // 将 UILabel 添加到视图
    view.addSubview(label1)
    view.addSubview(label2)
    view.addSubview(label3)
    
    // 禁用 Autoresizing Mask 转换
    label1.translatesAutoresizingMaskIntoConstraints = false
    label2.translatesAutoresizingMaskIntoConstraints = false
    label3.translatesAutoresizingMaskIntoConstraints = false
    
    // 设置约束
    NSLayoutConstraint.activate([
      // 第一个 UILabel
      label1.leadingAnchor.constraint(equalTo: view.leadingAnchor),
      label1.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
      label1.heightAnchor.constraint(equalToConstant: 60),
      label1.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1/3),
      
      // 第二个 UILabel
      label2.leadingAnchor.constraint(equalTo: label1.trailingAnchor),
      label2.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
      label2.heightAnchor.constraint(equalToConstant: 60),
      label2.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1/3),
      
      // 第三个 UILabel
      label3.leadingAnchor.constraint(equalTo: label2.trailingAnchor),
      label3.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
      label3.heightAnchor.constraint(equalToConstant: 60),
      label3.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1/3),
      label3.trailingAnchor.constraint(equalTo: view.trailingAnchor)
    ])
  }
  
  // 创建一个 UILabel
  private func createLabel(text: String, backgroundColor: UIColor) -> UILabel {
    let label = UILabel()
    label.text = text
    label.textColor = .white
    label.backgroundColor = backgroundColor
    label.textAlignment = .left
    label.font = UIFont.systemFont(ofSize: 40)
    label.numberOfLines = 1
    label.adjustsFontSizeToFitWidth = true
    label.minimumScaleFactor = 0.3
    label.baselineAdjustment = .none
    return label
  }
}
